package org.argetr.resim.proc.view;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.argetr.resim.ResimProjectEditor;
import org.argetr.resim.ViewerPage;
import org.argetr.resim.proc.ConnectionRule;
import org.argetr.resim.proc.data.Data;
import org.argetr.resim.proc.data.HistogramDA;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;

public class HistogramViewerPR extends ViewerPR {
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(HistogramDA.class, 0);
	}
	
	public Data process2() {
		HistogramDA data = getInputAsHistogramDA();
		
		JFrame frame = new JFrame();
		frame.setTitle("Histogram Viewer");
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	    if (workbenchWindow == null) {
		  return null;
	    }
			
		IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
		if (workbenchPage == null) {
		  return null;
		}
		
        int nBins = data.getNBins();
        int nBands = data.getNBands();
		for (int i = 0; i < nBands; i++) {
			DefaultXYDataset xySet = new DefaultXYDataset();
			double[][] points = new double[2][nBins];
			int[] histogram = data.getBins(i);
			for(int y = 0; y < nBins; y++){
					points[0][y] = y;
					points[1][y] = (double)histogram[y];
			}
			xySet.addSeries("HI", points);
			XYBarDataset series = new XYBarDataset(xySet, 2);
			JFreeChart chart = ChartFactory.createXYBarChart(
					"Histogram Chart", i + ". Band Bins", false, "Total Numbers", series,
					PlotOrientation.VERTICAL, true, false, false);
			XYPlot plot = (XYPlot) chart.getPlot();
			// set the range axis to display integers only...
			final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			ChartPanel panel = new ChartPanel(chart);			
			addViewer(panel, "Histogram Viewer");			
		}
	    return null;
	}
	@Override
	public Data process() {
		HistogramDA data = getInputAsHistogramDA();
        int nBins = data.getNBins();
        int nBands = data.getNBands();
		for (int i = 0; i < nBands; i++) {
			DefaultXYDataset xySet = new DefaultXYDataset();
			double[][] points = new double[2][nBins];
			int[] histogram = data.getBins(i);
			for(int y = 0; y < nBins; y++){
					points[0][y] = y;
					points[1][y] = (double)histogram[y];
			}
			xySet.addSeries("HI", points);
			XYBarDataset series = new XYBarDataset(xySet, 2);
			JFreeChart chart = ChartFactory.createXYBarChart(
					"Histogram Chart", (i+1) + ". Band Bins", false, "Total Numbers", series,
					PlotOrientation.VERTICAL, true, false, false);
			XYPlot plot = (XYPlot) chart.getPlot();
			// set the range axis to display integers only...
			final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
			rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
			ChartPanel panel = new ChartPanel(chart);
			addViewer(panel,(i+1)+". Band Histogram");
		}
		return null;
	}
	
	private void addViewer(JPanel panel,String title)
	{
		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	    IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
	    ResimProjectEditor rpe = (ResimProjectEditor)workbenchPage.getActiveEditor();
	    try {		    	
	    	ViewerPage viewer = new ViewerPage(rpe,panel,panel.toString(),title);
			rpe.addPage(viewer);
		} catch (PartInitException e1) {
			e1.printStackTrace();
		}
	}
	
}
